.Net Core 中同一接口多个实现依赖注入的问题

您所在的位置:网站首页 dotnet 依赖注入 .Net Core 中同一接口多个实现依赖注入的问题

.Net Core 中同一接口多个实现依赖注入的问题

2024-07-15 01:47| 来源: 网络整理| 查看: 265

创建一个接口,然后提供两个实现的类

需要添加 Microsoft.Extensions.DependencyInjection nuget包的引用

1 void Main() 2 { 3 var provider = new ServiceCollection() 4 .AddScoped() 5 .AddScoped() 6 .BuildServiceProvider(); 7 8 9 var nlog = provider.GetService(); 10 var serilog = provider.GetService(); 11 nlog.WriteLog(); 12 serilog.WriteLog(); 13 } 14 15 public interface ILogCore 16 { 17 void WriteLog(); 18 } 19 20 public class NlogCore : ILogCore 21 { 22 public void WriteLog() 23 { 24 Console.WriteLine("Writing logs by nlog"); 25 } 26 } 27 28 public class SerilogCore : ILogCore 29 { 30 public void WriteLog() 31 { 32 Console.WriteLine("Writing logs by serilog"); 33 } 34 }

运行结果如下:

Writing logs by serilog Writing logs by serilog

按照上面的写法,看到的结果并不是我们想要的,这个后面的注入覆盖了前面的,那我们该如何做到一个接口的多个实现呢?可以对代码进行稍微的调整,利用另一个扩展的方法:

1 void Main() 2 { 3 var provider = new ServiceCollection() 4 .AddScoped() 5 .AddScoped() 6 .AddScoped(factory => 7 { 8 Func accessor = key => 9 { 10 11 if (key.Equals("nlog"))//根据传入的key值,然后返回相应的实现类的实例 12 { 13 return factory.GetService(); 14 } 15 else if (key.Equals("serilog")) 16 { 17 return factory.GetService(); 18 } 19 else 20 { 21 throw new ArgumentNullException($"{key} is not supported!"); 22 } 23 }; 24 25 26 return accessor; 27 }) 28 .BuildServiceProvider(); 29 30 var logProvider = provider.GetService(); 31 var nlog = logProvider("nlog"); 32 var serilog = logProvider("serilog"); 33 nlog.WriteLog(); 34 serilog.WriteLog(); 35 } 36 37 public interface ILogCore 38 { 39 void WriteLog(); 40 } 41 42 public class NlogCore : ILogCore 43 { 44 public void WriteLog() 45 { 46 Console.WriteLine("Writing logs by nlog"); 47 } 48 } 49 50 public class SerilogCore : ILogCore 51 { 52 public void WriteLog() 53 { 54 Console.WriteLine("Writing logs by serilog"); 55 } 56 }

输出结果:

Writing logs by nlog Writing logs by serilog

这个正是我们想要的结果,这是通过使用微软提供的IOC容器进行的依赖注入,如果换成Autofac又当如何实现呢?

Autofac版同一接口多个实现的依赖注入的解决方案

需要添加以下nuget包的引用:

Microsoft.Extensions.DependencyInjection

Autofac.Extensions.DependencyInjection

还是以上面的代码为例,直接上代码:

1 void Main() 2 { 3 var collections = new ServiceCollection(); 4 var containerBuilder = new ContainerBuilder(); 5 containerBuilder.Populate(collections); 6 containerBuilder.RegisterType().Keyed(LogTargetEnum.Nlog); 7 containerBuilder.RegisterType().Keyed(LogTargetEnum.Serilog); 8 var container = containerBuilder.Build(); 9 var serivceProvider = new AutofacServiceProvider(container); 10 11 var logProvider = serivceProvider.GetService(); 12 var nlog = logProvider[LogTargetEnum.Nlog]; 13 var serilog = logProvider[LogTargetEnum.Serilog]; 14 15 nlog.WriteLog(); 16 serilog.WriteLog(); 17 } 18 19 public interface ILogCore 20 { 21 void WriteLog(); 22 } 23 24 public class NlogCore : ILogCore 25 { 26 public void WriteLog() 27 { 28 Console.WriteLine("Writing logs by nlog"); 29 } 30 } 31 32 public class SerilogCore : ILogCore 33 { 34 public void WriteLog() 35 { 36 Console.WriteLine("Writing logs by serilog"); 37 } 38 } 39 40 public enum LogTargetEnum 41 { 42 Nlog, 43 Serilog 44 }

 

输出结果:

Writing logs by nlog Writing logs by serilog

也能达到我们想要的结果

当然也还有其他的方式达到我们的目的,其他的方式请大家自己摸索和学习,也可以在下面留言,大家共同学习和进步!

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3